#include "ViewInfo.hpp"

using namespace BFT::order::statemanagement;

ViewInfo::ViewInfo(uint32_t _view, std::vector<boost::shared_ptr<ViewChangeCertificate> > _vcerts, bool _prim)
{
	primary = _prim;
	viewNo = _view;
	viewChanges = _vcerts;
	if((int)_vcerts.size() != BFT::Parameters::getOrderCount())
	{
	fprintf(stderr,"ViewInfo::ViewInfo says: unable to construct a vinfo object, vcerts of wrong size\n");
		exit(160);
	}
	
	pendingAcks.resize(_vcerts.size());

	missingViews.resize(BFT::Parameters::getOrderCount());
//	//TODO stop cheating
//	trees = new Vector<TreeRoot>();

	for(unsigned int i=0; i<viewChanges.size(); i++)
	{
		if (viewChanges.at(i).use_count()==0)
		{
			viewChanges.at(i).reset(new BFT::order::statemanagement::ViewChangeCertificate(viewNo,primary));
		}	
		
//		std::vector<boost::shared_ptr< BFT::order::messages::ViewChangeAck> > temp;
//		pendingAcks.at(i) = temp;
		pendingAcks.at(i) = std::vector<boost::shared_ptr< BFT::order::messages::ViewChangeAck> > ();
		pendingAcks.at(i).resize(viewChanges.size());
	}
	
	viewConfirmations.reset( new BFT::messages::Quorum<BFT::order::messages::ConfirmView>(BFT::Parameters::getOrderCount(),
											     BFT::Parameters::largeOrderQuorumSize(),
											     0 ) );			
	//TODO seems that for loops could be aggregated in une loop
	for(unsigned int i=0; i<viewChanges.size(); i++)
	{
		if (viewChanges.at(i).get()->getConfirmView().use_count() != 0)
			viewConfirmations.get()->addEntry(viewChanges.at(i).get()->getConfirmView());
	}
	
	cvCache.resize(BFT::Parameters::getOrderCount());
	
	certs.resize(BFT::order::Parameters::maxPeriods);
	certDigests.resize(BFT::order::Parameters::maxPeriods);
	histories.resize(BFT::order::Parameters::maxPeriods);
	for( int i=0; i< BFT::order::Parameters::maxPeriods; i++)
	{
		certs.at(i) = std::vector<boost::shared_ptr<Certificate> > ();
		certs.at(i).resize(BFT::order::Parameters::checkPointInterval);
		
		for(int j=0; j< BFT::order::Parameters::checkPointInterval; j++)
		{
			certs.at(i).at(j).reset( new Certificate(BFT::Parameters::getOrderCount(), 
								j == BFT::order::Parameters::checkPointInterval -1) );
		}
		
		std::vector<boost::shared_ptr< BFT::messages::Digest> > temp2;
		certDigests.at(i) = temp2;
		certDigests.at(i).resize(BFT::order::Parameters::checkPointInterval);
		
		std::vector< boost::shared_ptr< BFT::messages::HistoryDigest> > temp3;
		histories.at(i) = temp3;
		histories.at(i).resize(BFT::order::Parameters::checkPointInterval);
	}
	
	isCommitted = false;
	viewDefined = false;
}

void ViewInfo::observeViewChange(boost::shared_ptr<BFT::order::messages::ViewChange> _vc)
{
//	fprintf(stderr, "ViewInfo::observeViewChange --- before checking viweDefined. usecount %d\n", _vc.use_count());
	if(viewDefined)
	{
		fprintf(stderr, "ViewInfo::observeViewChange -view already defined. returning. usecount %d\n", (int)_vc.use_count());
		return;	
	}
//	fprintf(stderr, "ViewInfo::observeViewChange --- after checking viweDefined (else). usecount %d\n", _vc.use_count());
	
	
	if(_vc.get()->getView() != viewNo)
	{
		fprintf(stderr,"ViewInfo::observeViewChange says: trying to observe a vc fro a different view\n");
		exit(163);
	}
	
	uint32_t index = _vc.get()->getSendingReplica();
	if (viewChanges.at(index).use_count() != 0)
	{
		
		 boost::shared_ptr<BFT::messages::Digest> vc_digest (new BFT::messages::Digest( _vc.get()->getBytes(), DIGEST_SIZE ) );
	
		if (newViewMsg.use_count() == 0 ||
		    newViewMsg.get()->getChosenVCs().at(index).get()->equals( vc_digest )  )
		{
			viewChanges.at(index).get()->observeViewChange(_vc);
			missingViews.at(index) = false;
			bool res = false;
			
			if(newViewMsg.use_count() != 0 &&
			   newViewMsg.get()->getChosenVCs().at(index).use_count() != 0
		    	  )
			{
				populateVCTrees(_vc);
			}
			
			for(unsigned int i=0; i< missingViews.size(); i++)
			{
				res = res || missingViews.at(i);
			}
			
			if(!res && newViewMsg.use_count()>0)
			{
				defineView(true);
			} 
		}
		else
		{
			return;
		}
	}
	else
	{
		fprintf(stderr,"should have a certificate for replica %d\n",_vc.get()->getSendingReplica() );
		exit(164);
	}
	
	if(primary)
	{
		for(unsigned int i = 0 ; i< pendingAcks.size() ; i++)
		{
			if(pendingAcks.at(index).at(i).use_count() != 0)
			{
				addViewChangeAck(pendingAcks.at(index).at(i));
			}
		}
	}
}

void ViewInfo::addViewChangeAck(boost::shared_ptr<BFT::order::messages::ViewChangeAck> _vca)
{
//	fprintf(stderr, "ViewInfo::addViewChangeAck NIY!\n");
	if(!primary)
	{
		fprintf(stderr,"ViewInfo::addViewChangeAck says: only the primary accepts vc acks\n");
		exit(165);
	}
	
	if(viewDefined)
	{
		return;
	}
	
	if (_vca.get()->getViewNo() != viewNo )
	{
		fprintf(stderr,"ViewInfo::addViewChangeAck says: wrong view!\n");
		exit(165);
	}
	
	int index = _vca.get()->getChangingReplica();
	if( viewChanges.at(index).get()->getViewChange().use_count() != 0)
	{
//		fprintf(stderr,"ViewInfo::addviewchangeack says: calling addack on index %d\n",index);
		viewChanges.at(index).get()->addAck(_vca);
	}
	else
	{
		pendingAcks.at(index).at(_vca.get()->getSender()) = _vca;
	}
}

void ViewInfo::addNewView(boost::shared_ptr<BFT::order::messages::NewView> _nv)
{
	if (_nv.get()->getView() != viewNo)
	{
		fprintf(stderr,"ViewInfo::addnewview says : bad view\n");
		exit(178);
	}
	   
	clearTrees();
	
	if (viewDefined)
	{
		fprintf(stderr,"ViewInfo::addnewview says : already have a new view, should not be there\n");
		exit(179);
	}
	else
	{
		newViewMsg = _nv;
	    	std::vector<boost::shared_ptr< BFT::messages::Digest> > d = _nv.get()->getChosenVCs();
	    	int count = 0;
		for (unsigned int i = 0; i < viewChanges.size(); i++)
		{
			if (d.at(i).use_count() == 0)
			{
				viewChanges.at(i).get()->clear();
//				fprintf(stderr,"ViewInfo::addnewview says : clearing VC %d (not chosen)\n",i);
				
			}
			boost::shared_ptr<BFT::messages::Digest> vcd = viewChanges.at(i).get()->getVCDigest();
			if (vcd.use_count() == 0 || !d.at(i).get()->equals(vcd))
			{
				viewChanges.at(i).get()->clear();
				if (d.at(i).use_count() == 0)
				{
					missingViews.at(i) = false;
				}
				else
				{
					missingViews.at(i) = true;
					count++;
				}
			}
			else
			{
				viewChanges.at(i).get()->markFinal();
				missingViews.at(i) = false;
				populateVCTrees(viewChanges.at(i).get()->getViewChange());
			}
		}
		if (count == 0)
		defineView(true);
//		fprintf(stderr,"ViewInfo::addnewview says :View is now defined!\n");
	}
}

void ViewInfo::addConfirmation(boost::shared_ptr<BFT::order::messages::ConfirmView> _cv)
{
	if (!viewDefined)
	{
		fprintf(stderr,"ViewInfo::addconfirmation says: view not defined, caching the viewconfirmation message.\n");

		cvCache.at((int)_cv.get()->getSendingReplica()) = _cv;
	    	return;
	}
	if (_cv.get()->getViewNo() != viewNo)
	{
		fprintf(stderr,"ViewInfo::addconfirmation says: views should match\n");
		exit(177);
	}    
	if (_cv.get()->getSeqNo() != getNextSeqNo())
	{
		fprintf(stderr,"ViewInfo::addconfirmation says: sequence numbers should match\n");
		exit(178);
	}
	
	int index = (int)( _cv.get()->getSeqNo() - CPSeqNo);
	
	if (index > (int)(2*histories.at(0).size()))
	{
		fprintf(stderr,"ViewInfo::addconfirmation says: invalid sequence number\n");
		exit(179);
	}

	// Either cv.getNextSeqNo has a null history or is the last index
	// in the CP interval
	if (index == -1 
	    || (histories.at(in1(index-1)).at(in2(index-1)).use_count() != 0 
		&& (index  == (int)(2*histories.at(0).size()) 
		   || histories.at(in1(index)).at(in2(index)).use_count() == 0)))
	{

		viewConfirmations.get()->addEntry(_cv);
		if (viewConfirmations.get()->addEntry(_cv))
		{
			fprintf(stderr,"ViewInfo::addconfirmation says: added the confirmview message. view %d seq%d\n",
				_cv.get()->getViewNo(), _cv.get()->getSeqNo());
			_cv.get()->getHistory().get()->printInfo();	
		}
		else
		{
			fprintf(stderr,"ViewInfo::addconfirmation says: UNABLE TO ADD the confirmview message. view %d seq%d\n",
				_cv.get()->getViewNo(), _cv.get()->getSeqNo());		
			_cv.get()->getHistory().get()->printInfo();
		}
	}
	else
	{
		fprintf(stderr,"ViewInfo::addconfirmation says: confirmview does not match\n");
		exit(180);
	}
}

void ViewInfo::flushConfirmedCache()
{
	for (unsigned int i = 0; i < cvCache.size(); i++)
	{
		if (cvCache.at(i).use_count() != 0)
		{
			addConfirmation(cvCache.at(i));
	    	} 
	}
}

//Sets viewDefined to true if the view can be adequately defined
//(i.e. cp and request numbers and hashes)
//TODO: stop cheating on view changes!
boost::shared_ptr<BFT::order::messages::NewView> ViewInfo::generateNewViewMessage(boost::shared_ptr<BFT::BaseNode> _bn)
{
//	fprintf(stderr, "ViewInfo::geberateNewViewMessage NIY\n");
	// only the primary generates new view messages
	if (!primary)
	{
		fprintf(stderr, "ViewInfo::generateNewViewMessage says: only the primary generates new views!\n");
		exit(167);
	}
	
	if (newViewMsg.use_count() != 0)
	{
	    return newViewMsg;
	}
	
//	uint32_t cpSeqNo = 0;
//	uint32_t ppSeqNo = 0;

	int count = 0;
	std::vector< boost::shared_ptr<BFT::messages::Digest> > vcd;
	vcd.resize(viewChanges.size());
	
	for (unsigned int i = 0; i < viewChanges.size(); i++)
	{
		if (viewChanges.at(i).get()->hasAcks())
		{
			vcd.at(i) = viewChanges.at(i).get()->getVCDigest();
			populateVCTrees(viewChanges.at(i).get()->getViewChange());
			count++;
		}
	}
	
	if (count < BFT::Parameters::largeOrderQuorumSize())
	{
//		not enough view changes, returning an empty shared_ptr
		fprintf(stderr, "ViewInfo::generatenewviewmessage says: only %d acked viewchanges, need at least %d\n"
			,count,BFT::Parameters::largeOrderQuorumSize() );
		boost::shared_ptr<BFT::order::messages::NewView> temp;
		return temp;
	}
	
	//TODO
	// for now, do the simple thing and make sure that we keep the
	// largest view change message

	fprintf(stderr,"ViewInfo::generatenewviewmessage says : New View Definition needs to be fixed!\
	we're currently cheating by using the maximal VC out of 2f+1 that we have received.\n");

	int min = -1;
	long max = -1;

	fprintf(stderr, "****** count %d*******\n",count);	
	
	boost::shared_ptr<BFT::order::messages::ViewChange> vc;
	while (count > BFT::Parameters::largeOrderQuorumSize())
	{
		for (unsigned int i = 0; i < vcd.size(); i++)
		{
			if (count >= BFT::Parameters::largeOrderQuorumSize())
			{
				vc = viewChanges.at(i).get()->getViewChange();
				if (vc.use_count() != 0)
				{
					if (vc.get()->getPPSeqNo() <= max)
					{
						min = i;
					}    
					else
					{
						max = vc.get()->getPPSeqNo();
					}
				}
			}
		}
		
		vcd.at(min).reset();
		count --;
		fprintf(stderr,"**** min %d - max %d - quorum %d - count %d****\n"
			,min,(int)max,(int)BFT::Parameters::largeOrderQuorumSize(),(int)count);
		min = -1;
	}

	fprintf(stderr,"ViewInfo::generatenewviewmessage: vcd status: %d %d %d %d\n",
		vcd.at(0).use_count()!=0?1:0, vcd.at(1).use_count()!=0?1:0, 
		vcd.at(2).use_count()!=0?1:0, vcd.at(3).use_count()!=0?1:0 );

	newViewMsg.reset( new BFT::order::messages::NewView(viewNo, vcd, _bn.get()->getMyOrderIndex(),
				 BFT::Parameters::largeOrderQuorumSize() ) );
	_bn.get()->authenticateOrderMacArrayMessage(newViewMsg);

	//	defineView(root, subroot);
	defineView(NULL, NULL, false);
	return newViewMsg;
}

void ViewInfo::defineView(char* fake1, char* fake2, bool _not_primary)
{
	
	if(BFT::order::Parameters::maxPeriods !=2 )
	{
		fprintf(stderr, "ViewInfo::defineView(c,c,b) says: maxPeriod has to be == 2!\n");
		exit(168);
	}
	
	bool test =  false;

	for (unsigned int i = 0; i < missingViews.size(); i++)
	{
	    test = test || missingViews.at(i);
	}
	
	if (test)
	{
	    fprintf(stderr, "ViewInfo::defineView(c,c,p) says: should not be definign a view if we're still missing view changes\n");
	    exit(169);
	}
	
	int ind = -1;
	long max = -1;
	int count = 0;
	
	for(unsigned int i = 0; i < viewChanges.size(); i++)
	{
		boost::shared_ptr<BFT::order::messages::ViewChange> vc = viewChanges.at(i).get()->getViewChange();
		
		if (vc.use_count()!= 0 && (_not_primary || viewChanges.at(i).get()->hasAcks()))
		{
			if (vc.get()->getPPSeqNo() > max)
			{
				ind = i;
				max = vc.get()->getPPSeqNo();
				fprintf(stderr, " *+*+*+*+*+*+*+ Max: %d, ind: %d\n",(int)max,ind);
			}
			count++;
		}
	}
	    
	fprintf(stderr," *+*+*+*+*+*+*+ looked at %d vcs\n",count);

	boost::shared_ptr<BFT::order::messages::ViewChange> vc = viewChanges.at(ind).get()->getViewChange();
	committedCP = vc.get()->getCommittedCP(); //cheating
	committedCPDigest = committedCP.get()->getDigest(); //cheating
	if (committedCPDigest.use_count() == 0)
	{
		fprintf(stderr,"ViewInfo::defineView says: That should NEVER happen\n");
		exit(168);
	}
	CPSeqNo = vc.get()->getCommittedCPSeqNo();
	stableCP = vc.get()->getStableCP(); //cheating
	stableCPDigest = stableCP.get()->getDigest(); //cheating

	if (stableCPDigest.get()->equals(committedCPDigest))
	{
	    stableCP.reset();
	    stableCPDigest.reset();
	}
	
	nextSeqNo = vc.get()->getPPSeqNo();

	std::vector<boost::shared_ptr<BFT::messages::HistoryDigest> > hist = vc.get()->getHistories();
	std::vector<boost::shared_ptr<BFT::messages::CertificateEntry> > batch = vc.get()->getBatch(); 
	std::vector<boost::shared_ptr<BFT::messages::Digest> > dig;
	dig.resize(batch.size() );
	for(unsigned int i=0; i<batch.size(); i++)
	{
		dig.at(i) = batch.at(i).get()->getDigest();
	}
	unsigned int iterations = histories.size() * histories.at(0).size();
	for (unsigned int i = 0; i < iterations ; i++)
	{
		if (i < hist.size())
		{
			histories.at(in1(i)).at(in2(i)) = hist.at(i);
			//certs.at(in1(i)).at(in2(i)).reset( new Certificate(BFT::Parameters::getOrderCount()));
			certs.at(in1(i)).at(in2(i)).get()->setEntry(batch.at(i), CPSeqNo+i) ;
			certDigests.at(in1(i)).at(in2(i)) = dig.at(i);
	    	}
	    	else
	    	{
	    		//setting empty entry
//	    		boost::shared_ptr<BFT::messages::Entry> empty_entry
//			histories.at(in1(i)).at(in2(i)).reset();
//			certs.at(in1(i)).at(in2(i)).reset();
//			certDigests.at(in1(i)).at(in2(i)).reset();
	    	}
	}

	// if we have two full checkpoint intervals of requests, then
	// the new view should start with only one such intervals
	if (nextSeqNo == CPSeqNo+2*BFT::order::Parameters::checkPointInterval)
	{
	    	CPSeqNo = CPSeqNo + BFT::order::Parameters::checkPointInterval;
		committedCPDigest = stableCPDigest;
		committedCP = stableCP;
		stableCPDigest.reset();
		stableCP.reset();
		
		histories.at(0) = histories.at(1);
		certDigests.at(0) = certDigests.at(1);
		certs.at(0) = certs.at(1);
		histories.at(1).clear();
		histories.at(1).resize(histories.at(0).size());
		certDigests.at(1).clear();
		certDigests.at(1).resize(certDigests.at(0).size());
		certs.at(1).clear();
		certs.at(1).resize(certs.at(0).size());
		for(int j=0; j< BFT::order::Parameters::checkPointInterval; j++)
		{
			certs.at(1).at(j).reset( new Certificate(BFT::Parameters::getOrderCount(), 
								j == BFT::order::Parameters::checkPointInterval -1) );
		}
		
	}
	
	if (committedCPDigest.use_count() == 0)
	{
		fprintf(stderr,"ViewInfo::defineView says: we've got a problem with a full view change\n");
		exit(168);
	}
	
	viewDefined = true;
	fprintf(stderr,"ViewInfo::defineView says: view %d is now DEFINED!\n",viewNo);
}

std::vector<uint32_t> ViewInfo::getMissingCPs()
{
	fprintf(stderr, "ViewInfo::getMissingCPs NIY\n");
	std::vector<uint32_t> temp;
	return temp;
}

void ViewInfo::addCheckPointState(boost::shared_ptr<CheckPointState> _cps)
{
	fprintf(stderr, "ViewInfo::addCheckPointState NIY\n");
}

//add CP that I already have locally				
bool ViewInfo::addMyCP(boost::shared_ptr<CheckPointState> _cps)
{

	if (_cps.get()->getCurrentSequenceNumber() == CPSeqNo)
	{
		// if we dont ahve a committed cp and the candidate matches the stable digest
		if (committedCP.use_count() == 0 && 			// 
		    committedCPDigest.get()->equals(_cps.get()->getStableDigest()))
		{
			// then add the cp as committed cp
			committedCP = _cps;
//			fprintf(stderr,"OrderBaseNode::addmycp says: added as the committed CP\n");
			return true;
		}
//		else
//		{
//			fprintf(stderr, "not added because...");
//			if(committedCP.use_count() != 0 )
//				fprintf(stderr, " already have one ");
//			if(!committedCPDigest.get()->equals(_cps.get()->getStableDigest()))		
//				fprintf(stderr," different digest ");
//			fprintf(stderr, "\n");
//		}
	}
	else // stable seqno
	{
		// add the stable checkpoint if the histories match
		if (_cps.get()->getCurrentSequenceNumber() == 
		    CPSeqNo + BFT::order::Parameters::checkPointInterval)
		{
			// if we dont have a stable cp yet and 
			// ( the digest is null or the digest equals the candidate digest) and
			// and the histories match the sequence,
			// then add the CP as stable
			if ( stableCP.use_count() == 0 &&
			     (stableCPDigest.use_count() == 0 || stableCPDigest.get()->equals(_cps.get()->getStableDigest())) &&
			     histories.at(0).at(histories.at(0).size()-1).get()->equals(_cps.get()->getHistory()))
			{
				stableCP = _cps;
//				fprintf(stderr,"OrderBaseNode::addmycp says: added as the stable CP\n");
				return true;
			}
		}
	}
//	fprintf(stderr,"OrderBaseNode::addmycp says: NOT added\n");
	return false;
}
				
std::vector<bool> ViewInfo::getMissingRequests()
{
//	fprintf(stderr, "ViewInfo::getMissingRequests NIY\n");
//	std::vector<bool> temp;
//	return temp;	
	std::vector<bool> miss;
	miss.resize(2*certs.at(0).size());
	for (unsigned int i = 0; i < miss.size(); i++)
	{
		if (certs.at(in1(i)).at(in2(i)).get()->getCertEntry().use_count() == 0
			&& certDigests.at(in1(i)).at(in2(i)).use_count() != 0)
		{
			miss.at(i) = true;
		}
	    	else
	    	{
			miss.at(i) = false;
		}
	}
	return miss;
}				

bool ViewInfo::addEntry(boost::shared_ptr<BFT::messages::CertificateEntry> _entry, uint32_t _seqno)
{
	fprintf(stderr, "ViewInfo::addEntry NIY\n");
	return true;
}
				
bool ViewInfo::setCertificate(boost::shared_ptr<statemanagement::Certificate> _entry, uint32_t _seqno)
{
	fprintf(stderr, "ViewInfo::setCertificate NIY\n");
	return true;
}
				
boost::shared_ptr<BFT::messages::CertificateEntry> ViewInfo::getEntry(uint32_t _seqno)
{
	fprintf(stderr, "ViewInfo::getEntry NIY\n");
	boost::shared_ptr<BFT::messages::CertificateEntry> temp;
	return temp;
}

bool ViewInfo::isComplete()
{
	bool res = false;

	if (!definedView())
	{
		fprintf(stderr,"ViewInfo::isComplete says: not defined, returning false\n");
		return false;
	}

	// the committed checkpoint is locally stable
	res = committedCP.use_count() != 0 && committedCP.get()->isStable();
	if (!res)
	{
		fprintf(stderr,"ViewInfo::isComplete says: committed cp not ready, returning false\n");
		return false;
	}
	
	// the stable checkpoint is locally stable
	res = stableCPDigest.use_count() == 0 
	      || (stableCP.use_count() != 0 && stableCP.get()->isStable());
	if (!res)
	{
		fprintf(stderr,"ViewInfo::isComplete says: stable cp not ready, returning false\n");
		return false;
	}
	// check that we have all of the requisite pieces.
	for(unsigned int i = 0; i < nextSeqNo-CPSeqNo; i++)
	{
		if (certDigests.at(in1(i)).at(in2(i)).use_count() != 0 
		    && certs.at(in1(i)).at(in2(i)).get()->getCertEntry().use_count() == 0)
		{
			fprintf(stderr,"ViewInfo::isComplete says: missing a certificate entry, returning false\n");
			return false;
		}
	}
	return true;
}

boost::shared_ptr<CheckPointState> ViewInfo::getWorkingState()
{
	if (workingState.use_count() != 0)
	{
		return workingState;
	}
	
	workingState.reset( new CheckPointState(getStableCP()) );
	uint32_t base = workingState.get()->getBaseSequenceNumber();
	for (unsigned int i = 0; i + base < getNextSeqNo(); i++)
	{
		workingState.get()->addNextBatch(certs.at(1).at(i).get()->getCertEntry(), i+base);
	}

	if (workingState.get()->getCurrentSequenceNumber() !=  getNextSeqNo())
	{
		fprintf(stderr,"ViewInfo::getWorkingState says: misalignment!\n");
		exit(177);
	}
	
	return workingState;
}

boost::shared_ptr<BFT::messages::HistoryDigest> ViewInfo::getLastHistory()
{
	int ind = (int)(nextSeqNo-(CPSeqNo+1));
	if (ind > 0)
	{
		return histories.at(in1(ind)).at(in2(ind));
	}
	else
	{
		return committedCP.get()->getHistory();
	}
}

void ViewInfo::populateVCTrees(boost::shared_ptr<BFT::order::messages::ViewChange> _vc)
{
	fprintf(stderr, "ViewInfo::populateVCTrees NIY\n");
}

void ViewInfo::defineView(bool _notPrimary)
{
	//fprintf(stderr, "ViewInfo::defineView(bool) NIY\n");
	defineView(NULL, NULL, _notPrimary);
}

void ViewInfo::clearTrees()
{
	fprintf(stderr, "ViewInfo::clearTrees() NIY\n");
}


int ViewInfo::in1(int i)
{
	if (i >= 2*BFT::order::Parameters::checkPointInterval)
	{
		fprintf(stderr,"ViewInfo::in1 says : invalid index %d\n",i);
		exit(169);
	}
	return i / BFT::order::Parameters::checkPointInterval;
}

int ViewInfo::in2(int i)
{
	if (i >= 2*BFT::order::Parameters::checkPointInterval)
	{
		fprintf(stderr,"ViewInfo::in2 says : invalid index %d\n",i);
		exit(170);
	}
	return i % BFT::order::Parameters::checkPointInterval;
}
